package com.summer.campussct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.campussct.mapper.VehicleAuditMapper;
import com.summer.campussct.pojo.VehicleAudit;
import com.summer.campussct.pojo.dto.RestResult;
import com.summer.campussct.pojo.dto.VehicleInfoDto;
import com.summer.campussct.service.IVehicleAuditService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆审核表 服务实现类
 * </p>
 *
 * @author Mr.Chen
 * @since 2024-05-05
 */
@Service
public class VehicleAuditServiceImpl extends ServiceImpl<VehicleAuditMapper, VehicleAudit> implements IVehicleAuditService {

    @Override
    public RestResult commitAudit(long id) {
        // 只有状态为待提交才能提交审核
        VehicleAudit vehicleAudit = this.getOne(new LambdaQueryWrapper<VehicleAudit>()
                .select(VehicleAudit::getAuditStatus)
                .eq(VehicleAudit::getVid, id));
        if (vehicleAudit.getAuditStatus() != 20001){
            throw new RuntimeException("审核状态非法");
        }

        boolean update = this.update(new LambdaUpdateWrapper<VehicleAudit>()
                .set(VehicleAudit::getAuditStatus, 20002)
                .eq(VehicleAudit::getVid, id));
        if (!update) {
            throw new RuntimeException("提交审核失败");
        }
        return RestResult.success();
    }

    @Override
    public RestResult passAudit(long id) {
        // 只有状态为待审核才能审核
        VehicleAudit vehicleAudit = this.getOne(new LambdaQueryWrapper<VehicleAudit>()
                .select(VehicleAudit::getAuditStatus)
                .eq(VehicleAudit::getVid, id));
        if (vehicleAudit.getAuditStatus() != 20002){
            throw new RuntimeException("审核状态非法");
        }
        boolean update = this.update(new LambdaUpdateWrapper<VehicleAudit>()
                .set(VehicleAudit::getAuditStatus, 20004)
                .eq(VehicleAudit::getVid, id));
        if (!update) {
            throw new RuntimeException("{审核通过} 操作失败");
        }
        return RestResult.success();
    }

    @Override
    public RestResult rejectAudit(long id, String auditAdvice) {
        // 只有状态为待审核才能审核
        VehicleAudit vehicleAudit = this.getOne(new LambdaQueryWrapper<VehicleAudit>()
                .select(VehicleAudit::getAuditStatus)
                .eq(VehicleAudit::getVid, id));
        if (vehicleAudit.getAuditStatus() != 20002){
            throw new RuntimeException("审核状态非法");
        }
        boolean update = this.update(new LambdaUpdateWrapper<VehicleAudit>()
                .set(VehicleAudit::getAuditStatus, 20003)
                .set(VehicleAudit::getAuditAdvice, auditAdvice)
                .eq(VehicleAudit::getVid, id));
        if (!update) {
            throw new RuntimeException("{审核驳回} 操作失败");
        }
        return RestResult.success();

    }
}
